package Servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Service.UserService;
import pojo.PageBean;
import pojo.User;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method.equals("reg")) {
			reg(request,response);
		}else if(method.equals("login")) {
			login(request,response);
		}else if(method.equals("showAll")) {
			showAll(request,response);
		}else if(method.equals("update")) {
			update(request,response);
		}else if(method.equals("up")) {
			up(request,response);
//		}else if(method.equals("Up")){
//			Up(request,response);
//		}else if(method.equals("updatepass")){
//			updatepass(request,response);
		
		}
	}

	
	private void showAll(HttpServletRequest request,HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	int currentPage = Integer.parseInt(request.getParameter("currentPage"));
    	int pageSize=1;
    	UserService ps = new UserService();
    	PageBean<User> pb = ps.show(currentPage, pageSize);
    	request.setAttribute("pb", pb);
    	try {
			try {
				request.getRequestDispatcher("6.jsp").forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	public void login(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String username=request.getParameter("username");
		String pwd=request.getParameter("pwd");
		System.out.println(username);
		System.out.println(pwd);
		User c = new User();
		c.setUsername(username);
		c.setPwd(pwd);
		UserService cservice = new UserService();
		User cu=cservice.login(c);
		if(cu!=null) {
			HttpSession session=request.getSession(true);
			session.setAttribute("c",cu);
			try {
				response.sendRedirect("index.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				response.sendRedirect("login.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void reg(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name=request.getParameter("name");
		String username=request.getParameter("username");
		String pwd=request.getParameter("pwd");
		User c=new User();
		c.setName(name);
		c.setUsername(username);
		c.setPwd(pwd);
		UserService uservice=new UserService();
		int i=uservice.reg(c);
		if(i>0){
			try {
				response.sendRedirect("login.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				response.sendRedirect("reg.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void up(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int id=Integer.parseInt(request.getParameter("id"));
		UserService pservice = new UserService();
		User p = pservice.getUser(id);
		if(p!=null) {
			HttpSession session=request.getSession(true);
			session.setAttribute("P", p);
			try {
				response.sendRedirect("UpdateUser.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
//	private void Up(HttpServletRequest request, HttpServletResponse response) {
//		// TODO Auto-generated method stub
//		int id=Integer.parseInt(request.getParameter("id"));
//		UserService pservice = new UserService();
//		User pb = pservice.getoneUser(id);
//		if(pb!=null) {
//			HttpSession session=request.getSession(true);
//			session.setAttribute("Pb", pb);
//			try {
//				response.sendRedirect("updatepass.jsp");
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}
	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int id=Integer.parseInt(request.getParameter("id"));
		String identity=request.getParameter("identity");
		String name=request.getParameter("name");
		String degree=request.getParameter("degree");
		String position = request.getParameter("position");
		String onesubject=request.getParameter("onesubject");
		String twosubject=request.getParameter("twosubject");
		System.out.println(id);
//		Part part;
//		try {
//			part = request.getPart("p_image");
//			String filename=part.getSubmittedFileName();
//			String str = this.getServletContext().getRealPath("image");
//			File f=new File(str);
//			if (!f.exists()){
//				f.mkdir();
//			}
//		    String name1 = UUID.randomUUID()+filename;
//		    img="image/"+name1;
//			part.write(str+"/"+name1);
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//			
//		} catch (ServletException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		User p =new User();
		p.setId(id);
		p.setIdentity(identity); 
		p.setName(name);
		p.setDegree(degree);
		p.setPosition(position);
		p.setOnesubject(onesubject);
		p.setTwosubject(twosubject);
		UserService pservice=new UserService();
		int i = pservice.update(p);
		if(i>0) {
			try {
				response.sendRedirect("http://localhost:8080/kygl/UserServlet?method=showAll&currentPage=1");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				response.sendRedirect("update.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}
//	private void updatepass(HttpServletRequest request, HttpServletResponse response) {
//		// TODO Auto-generated method stub
//		try {
//			request.setCharacterEncoding("utf-8");
//		} catch (UnsupportedEncodingException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		int id=Integer.parseInt(request.getParameter("id"));
//		String pwd=request.getParameter("Pwd");
////		Part part;
////		try {
////			part = request.getPart("p_image");
////			String filename=part.getSubmittedFileName();
////			String str = this.getServletContext().getRealPath("image");
////			File f=new File(str);
////			if (!f.exists()){
////				f.mkdir();
////			}
////		    String name1 = UUID.randomUUID()+filename;
////		    img="image/"+name1;
////			part.write(str+"/"+name1);
////		} catch (IOException e1) {
////			// TODO Auto-generated catch block
////			e1.printStackTrace();
////			
////		} catch (ServletException e1) {
////			// TODO Auto-generated catch block
////			e1.printStackTrace();
////		}
//		
//		User pb =new User();
//		pb.setId(id);
//		pb.setPwd(pwd); 
//		UserService pservice=new UserService();
//		int i = pservice.updatepass(pb);
//		if(i>0) {
//			try {
//				response.sendRedirect("http://localhost:8080/kygl/UserServlet?method=showAll&currentPage=1");
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}else {
//			try {
//				response.sendRedirect("updatepass.jsp");
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

}